package chixing.day16.homework.work2;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class FlowTest2 {
    //正常的数据
   static List<Record> normalRecords = new ArrayList<>();
    //异常的数据
   static List<Record> abnormalRecords = new ArrayList<>();

    public static void main(String[] args){
        String filePath = "E:\\2025-cx\\project\\PJ\\J01\\Javase\\Temp\\flow1.txt";
        readFlow(filePath);
        System.out.println("正常数据");
        normalRecords.forEach(System.out::println);
        System.out.println("异常数据");
        abnormalRecords.forEach(System.out::println);
    }

    public static void readFlow(String filePath){
        //1.创建输入流
        //bufferRead可以一行一行读取
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        try {
            String title = bufferedReader.readLine();//第一行标题不需要
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String line;
        try {//一行一行读取，为空的时候就是读完了
            while ((line = bufferedReader.readLine()) != null){
                Record record = fun(line);
                if (record.getTransactionDate()==null){
                    //就是异常数据
                    abnormalRecords.add(record);
                }else {//正常数据
                    normalRecords.add(record);
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }

    public static Record fun(String line){
        Record record = null;
        String[] strings = line.split("\\|");//将拿到的数据以 | 分割
        //将读取的数据存起来
        String recordId = strings[0];
        String userName = strings[1];
        String institutionId = strings[2];
        char gender =  strings[3].equals("1") ? 'M':'F';
        String serialNumber =  strings[4];
        String transactionAmountStr = strings[6];
        //获取为String类型转换为Double类型
        double transactionAmount = Double.valueOf(transactionAmountStr);
        String LocalDateTimeStr = strings[5];
        //获取出来的是String类型要转换为LocalDateTime类型
        //判断是否是异常数据
        if (!LocalDateTimeStr.equals("null")){//不是异常数据，就转换类型
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            LocalDateTime transactionDate = LocalDateTime.parse(LocalDateTimeStr, formatter);
            record = new Record(recordId, userName, institutionId, gender, serialNumber, transactionDate, transactionAmount);
        }else {//是异常数据日期对象设为null
            record = new Record(recordId, userName, institutionId, gender, serialNumber, null, transactionAmount);
        }
        return record;
    }
}
